import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

/* 示例:
    EcStatisticViewModel(
      statistic: -10000,
      isCurrency: true,
      withName: '',

      toStringAsFixed: 3,
    ).toRichText,
 */

/// 设计用于[ListSectionView] 后续可以用于首页的数据显示
class MyStatisticViewModel {

  /// 显示的数据
  late final num statistic;

  ///整数部分的textStyle
  late final TextStyle statisticTS;

  /// 小数部分的TS
  late final TextStyle statisticDecimalTS;

  ///前缀,无数据不显示
  late final String prefix;
  late final TextStyle prefixTS;

  /// 后缀,无数据不显示
  late final String suffix;
  late final TextStyle suffixTS;

  ///如果不能显示，显示[hint]
  late final String hint;
  late final TextStyle hintTS;

  /// 货币模式
  /// 1234 -> 1,234.00
  late final bool isCurrency;

  /// 货币前缀，可以使用￥ $等符号
  /// 1234 + (withName:'EU') => EU1,234.00
  late final String withName;

  /// 货币符号和负号使用这个TextStyle
  late final TextStyle withNameTS;

  ///小数后显示多少位
  late final int toStringAsFixed;

  ///间距
  late final StrutStyle strutStyle;

  MyStatisticViewModel({
    this.hint = '',
    required this.prefix,
    required this.statistic,
    required this.suffix,
    this.hintTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.prefixTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.statisticTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.statisticDecimalTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.suffixTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.withNameTS = const TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
    this.isCurrency = true,
    this.strutStyle = const StrutStyle(forceStrutHeight: false),
    this.withName = '',
    this.toStringAsFixed = 2,
  });

  Widget get toRichText {
    String numberPart = isCurrency
        ? NumberFormat.currency(
            name: '',
            decimalDigits: toStringAsFixed,
          ).format(statistic.abs())
        : statistic.abs().toStringAsFixed(toStringAsFixed);
    String decimalPart = "";

    if (toStringAsFixed > 0) {
      String temp = numberPart;
      numberPart = temp.split('.').first;
      decimalPart = '.${temp.split('.').last}';
    }
    return Text.rich(
      TextSpan(
        children: [
          TextSpan(text: prefix, style: prefixTS),
          if (statistic.isNegative) TextSpan(text: '-', style: withNameTS),
          if (withName.isNotEmpty)
            TextSpan(
              text: withName,
              style: withNameTS,
            ),
          TextSpan(
            text: numberPart,
            style: statisticTS,
          ),
          TextSpan(
            text: decimalPart,
            style: statisticDecimalTS,
          ),
          TextSpan(
            text: suffix,
            style: suffixTS,
          ),
        ],
      ),
      strutStyle: strutStyle,
      style: const TextStyle(textBaseline: TextBaseline.alphabetic),
    );
    }
}
